package com.easy.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy.common.bean.Order;
import com.easy.common.bean.OrderItems;
import com.easy.common.service.IOrderService;
import com.easy.common.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @PostMapping
    public ResultData insert(@RequestBody Order order) {
        Order result = orderService.insert(order);
        return ResultData.success(result);
    }

    @PutMapping
    public ResultData update(@RequestBody Order order) {
        Order result = orderService.update(order);
        return ResultData.success(result);
    }

    @PutMapping("/transact/{id}")
    public ResultData transact(@PathVariable Integer id) {
        Order result = orderService.transact(id);
        if (result != null) {
            return ResultData.success(result);
        }
        return ResultData.fail();
    }

    @PutMapping("/cancel/{id}")
    public ResultData cancel(@PathVariable Integer id) {
        Order result = orderService.cancel(id);
        if (result != null) {
            return ResultData.success(result);
        }
        return ResultData.fail();
    }

    @GetMapping("/{id}")
    public ResultData getByID(@PathVariable Integer id) {
        Order result = orderService.getByID(id);
        return ResultData.success(result);
    }

    @GetMapping("/user/{userid}")
    public ResultData getListByUserID(@RequestBody Page<Order> page, @PathVariable Integer userid) {
        IPage<Order> result = orderService.getListByUserID(page, userid);
        return ResultData.success(result);
    }
    @GetMapping("/customer/{customerid}")
    public ResultData getListByCustomerID(@RequestBody Page<Order> page, @PathVariable Integer customerid) {
        IPage<Order> result = orderService.getListByCustomerID(page, customerid);
        return ResultData.success(result);
    }

    @PostMapping("/item")
    public ResultData insertItem(@RequestBody OrderItems orderItems) {
        OrderItems result = orderService.insertItem(orderItems);
        return ResultData.success(result);
    }

    @PutMapping("/item")
    public ResultData updateItem(@RequestBody OrderItems orderItems) {
        OrderItems result = orderService.updateItem(orderItems);
        return ResultData.success(result);
    }

    @DeleteMapping("/item/{id}")
    public ResultData deleteItem(@PathVariable Integer id) {
        int result = orderService.deleteItem(id);
        if (result > 0) {
            return ResultData.success();
        }
        return ResultData.fail();
    }
}